package com.gopay.bis.web.controller.ebank;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.hxbc.ebank.pay.req.HXEB2CReqPay;

/**
 * 
 * HxbcB2cServerRes.java
 * 
 * @author LiLiLi
 */
@Controller
@Scope("prototype")
public class HxbcB2cServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog("HXBC_LOG");

	@Override
	@RequestMapping(value = "/HxbcB2cServerRes")
	protected ModelAndView execute(HttpServletRequest request,
			HttpServletResponse response) {
		logger.error("--------------- HxbcB2cServerRes 接收HXBC B2C支付回应处理开始 ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		String xml = "";
		InputStream in = null;
		BufferedInputStream buf = null;
		HXEB2CReqPay message = null;
		try {
			StringBuffer info = new StringBuffer();
			in = request.getInputStream();
			buf = new BufferedInputStream(in);
			byte[] buffer = new byte[1024];
			int iRead;
			while ((iRead = buf.read(buffer)) != -1) {
				info.append(new String(buffer, 0, iRead, "UTF-8"));
			}
			xml = info.toString();
			message = new HXEB2CReqPay();
			logger.info("服务端接收到字符串为：xml==="+xml);
			message.setSignMessage(xml);
			// 银行业务产品00个人网银支付
			message.setBankBusProd(BisBankBusinessProd._00);
			// 银行接口通道信息 00: 直连
			message.setBankIntrChann(BisBankInterfaceChannel._00);
			message.setBank(BankConstants.BANK_LOGO_HXBC);
			message.setFront(false);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("华夏B2C返回密文处理异常： ", e);
		} finally {
			try {
				if (null != in) {
                    in.close();
                }
				if (null != buf) {
                    buf.close();
                }
			} catch (IOException e) {
				logger.error("关闭IO流异常： ", e);
			}
		}

		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		Map<String, String> param = (Map<String, String>) result
				.getModel("isNotifyCashier");
		/**
		 * 订单号
		 */
		String gopayOrderID = param.get("orderId");
		/**
		 * 订单金额
		 */
		String orderAmt = param.get("amt");
		/**
		 * 支付状态
		 */
		String payStatus = param.get("payStatus");
		if ("00".equals(payStatus)) {
			orderInfo.setOrderId(gopayOrderID);
			orderInfo.setAmt(orderAmt);
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("00");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			orderInfo.setOrderId(gopayOrderID);
			orderInfo.setAmt(orderAmt);
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg("");
		}
		return orderInfo;
	}
	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		return true;
	}
}
